package com.marvels.loanapply.mqueue.service.impl;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.loanapply.dto.LoanApplyInfoDTO;
import com.marvels.loanapply.mqueue.service.IRabbitMQService;
@Service
public class RabbitMQServiceImpl implements IRabbitMQService {
	@Autowired
	private AmqpTemplate amqpTemplate;
	
	@Value("${platform.name.prefix}")
	private String platformNamePrefix;
	
	/**推送消息到rabbitMQ
	 * @author 何杰
	 * 2018年4月21日
	 * @param key
	 * @param content
	 * @throws MarvelsBusinessException
	 */
	@Override
	public void send(String key, LoanApplyInfoDTO dto) throws MarvelsBusinessException {
		// TODO 自动生成的方法存根
		key = platformNamePrefix + key;
		MarvelsLogUtil.getInstance().info("开发发送...."+dto.toString()); 
		amqpTemplate.convertAndSend(key,dto.getLoanId().getBytes());
		MarvelsLogUtil.getInstance().info("开发完毕...."+dto.toString());
	}
	public void sendString(String key,LoanApplyInfoDTO dto)throws MarvelsBusinessException{
		MarvelsLogUtil.getInstance().info("开发发送...."+dto.toString()); 
		String json = "{\"loanId\":\""+dto.getLoanId()+"\",\"status\":\""+dto.getStatus()+"\",\"originalState\":\""+dto.getOldStatus()+"\"}";
		amqpTemplate.convertAndSend(key,json);
		MarvelsLogUtil.getInstance().info("开发完毕json...."+json);
	}
//	public static void main(String[] args){
//		String MQname ="loan_apply";
//		com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory();
//		factory.setHost("10.0.17.30");
//		factory.setUsername("admin");
//		factory.setPassword("12345");
//		factory.setPort(5672);
//		com.rabbitmq.client.Connection connection = null;
//		try {
//			connection = factory.newConnection();
//			com.rabbitmq.client.Channel channel = connection.createChannel();
//			channel.queueDeclare(MQname, true, false, false, null);
//			LoanApplyInfoDTO dto = new LoanApplyInfoDTO();
//			dto.setLoanCity("123");
//			dto.setLoanId("11111");
//			channel.basicPublish("", MQname, null,  dto.toString().getBytes());
//			channel.close();
//			connection.close();
//		}catch(Exception e){
//			MarvelsLogUtil.getInstance().error("",e);
//		}
//	}

}
